package com.qiangesoft.easyexcel.controller;

import com.alibaba.excel.EasyExcel;
import com.alibaba.excel.util.ListUtils;
import com.qiangesoft.easyexcel.read.ExcelRead;
import com.qiangesoft.easyexcel.read.ReadData;
import com.qiangesoft.easyexcel.write.ExcelWrite;
import com.qiangesoft.easyexcel.write.WriteData;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.net.URLEncoder;
import java.nio.charset.StandardCharsets;
import java.util.Date;
import java.util.List;

/**
 * easyExcel 控制器
 *
 * @author qiangesoft
 * @date 2024-04-10
 */
@RestController
@RequestMapping("/easyExcel")
public class EasyExcelController {

    private static final String fileName = "demo/demo.xlsx";

    @GetMapping("/read")
    public List<ReadData> read() throws IOException {
        ExcelRead.read(fileName);
        ExcelRead.read(fileName, 0);
        ExcelRead.read(fileName, 0, 1);
        ExcelRead.readExtra(fileName);
        ExcelRead.readBuiltIn(fileName);
        ExcelRead.readAnonymous(fileName);
        ExcelRead.readAllSheet(fileName);
        ExcelRead.readManySheet(fileName);
        List<ReadData> readDataList = ExcelRead.readSync(fileName);
        return readDataList;
    }

    @GetMapping("/write")
    public void write() throws IOException {
        ExcelWrite.write("D:/excel/write.xlsx");
        ExcelWrite.write1("D:/excel/write1.xlsx");
        ExcelWrite.write2("D:/excel/write2.xlsx");
        ExcelWrite.writeRepeat("D:/excel/writeRepeat.xlsx");
        ExcelWrite.writeSameSheet("D:/excel/writeSameSheet.xlsx");
        ExcelWrite.writeManySheet("D:/excel/writeManySheet.xlsx");
        ExcelWrite.writeWithImage("D:/excel/writeWithImage.xlsx");
        ExcelWrite.writeCellData("D:/excel/writeCellData.xlsx");
        ExcelWrite.writeWithTemplate("D:/excel/writeWithTemplate.xlsx", "demo/demo.xlsx");
        ExcelWrite.writeTable("D:/excel/writeTable.xlsx");
        ExcelWrite.writeComment("D:/excel/writeComment.xlsx");
    }

    @GetMapping("/download")
    public void download(HttpServletResponse response) throws IOException {
        response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
        response.setCharacterEncoding("utf-8");
        String fileName = URLEncoder.encode("测试", StandardCharsets.UTF_8).replaceAll("\\+", "%20");
        response.setHeader("Content-disposition", "attachment;filename*=utf-8''" + fileName + ".xlsx");

        EasyExcel.write(response.getOutputStream(), WriteData.class).sheet("模板").doWrite(data());
    }

    private List<WriteData> data() {
        List<WriteData> list = ListUtils.newArrayList();
        for (int i = 0; i < 10; i++) {
            WriteData data = new WriteData();
            data.setString("字符串" + 0);
            data.setDate(new Date());
            data.setDoubleData(0.56);
            list.add(data);
        }
        return list;
    }

}
